package edu.kit.aifb.swala.data.retriever;

import java.io.InputStream;
import java.util.Iterator;

import com.hp.hpl.jena.query.Dataset;
import com.hp.hpl.jena.query.ReadWrite;
import com.hp.hpl.jena.sparql.core.Quad;
import com.hp.hpl.jena.tdb.TDBFactory;
import com.hp.hpl.jena.tdb.TDBLoader;
import com.hp.hpl.jena.tdb.base.file.Location;
import com.hp.hpl.jena.tdb.sys.TDBInternal;
import com.hp.hpl.jena.util.FileManager;

public class LDRetriever {
	Location location = null;

	public LDRetriever() {
		location = new Location("swalaDB/Dataset1");
	}

	public void populateDataset(String inputFileName) {
		FileManager fm = FileManager.get();
		fm.addLocatorClassLoader(LDRetriever.class.getClassLoader());
		InputStream in = fm.open(inputFileName);

		// Load some initial data
		TDBLoader.load(TDBInternal.getBaseDatasetGraphTDB(TDBFactory
				.createDatasetGraph(location)), in, false);
	}

	public void test() {
        Dataset dataset = TDBFactory.createDataset(location);
        dataset.begin(ReadWrite.READ);
        try {
            Iterator<Quad> iter = dataset.asDatasetGraph().find();
            int count = 0;
            while ( iter.hasNext() ) {
                iter.next();
                count++;
                //System.out.println(quad);
            }
            System.out.println(count);
        } finally {
            dataset.end();
        }
		
		dataset.end();

	}
}
